using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class TitanBehavior : EntityBehavior
{
	public const int DefaultTitanHeight = 8;

	public float cameraUpOffset = 8f;

	public Transform topTitanPart;

	public Transform bottomTitanPart;

	public Transform topTitanPartPosition;

	public Transform leftArmTransform;

	public Transform rightArmTransform;

	public Quaternion initialArmsRotation;

	public TitanMecanimWrapper topMecanimWrapper;

	public TitanMecanimWrapper bottomMecanimWrapper;

	public TitanController titanController;

	private bool weaponsInitialized;

	[Space(10f)]
	public bool mainWeaponsIsHoming;

	public bool additionalWeaponsIsHoming;

	[Space(10f)]
	public float timeBetweenAdditWeaponShots;

	private float additWeaponsCurrentTimeShooting;

	[Space(10f)]
	public float timeBetweenMainWeaponShots;

	private float mainWeaponsCurrentTimeShooting;

	public bool cycleShootAnimation;

	[Space(10f)]
	public List<BaseWeapon> mainWeapons = new List<BaseWeapon>();

	public List<string> mainWeaponsAnimations;

	private int currentShootingMainWeapon;

	public string mainWeaponReloadAnimation;

	public List<BaseWeapon> additionalWeapons = new List<BaseWeapon>();

	public List<string> additionalWeaponsAnimations;

	private int currentShootingAdditionalWeapon;

	public string additionalWeaponReloadAnimation;

	public float fillJetpackFuelWithTime = 10f;

	public float spendJetpackFuelWithTime = 20f;

	public float jetpackFuel = 100f;

	public ParticleSystem[] jetpackParticles;

	public Texture blueTexture;

	public Texture redTexture;

	public AudioSource oneShotSource;

	public AudioClip fallAudioClip;

	public AudioClip deathAudioClip;

	public AudioSource jatpackSource;

	public AudioSource moveSource;

	public AudioSource brockenSource;

	public AudioClip[] impactClips;

	public BoxCollider colliderForCatching;

	private List<float> initialMainWeapons = new List<float>();

	private List<float> initialAdditionalWeapons = new List<float>();

	private int initialMaxHealth;

	public float initialMaxSpeed;

	[HideInInspector]
	public float jumpSpeed = 12.5f;

	private TitanTargetDetector _titanDetector;

	private bool renderersCollected;

	private Renderer[] _allTopRenderers;

	public bool mainWeaponsShooting;

	public float mainWeaponsMaxTimeToReload = 10f;

	private float mainWeaponsCurrentTimeToReload;

	[HideInInspector]
	public bool alreadyShootingMainWeapons;

	public float reloadMainWeaponsTime = 3f;

	private bool reloadingMainWeapons;

	private bool canShootAdditionalWeapon = true;

	public bool additionalWeaponsShooting;

	public bool repeatAdditionalWeaponsShooting;

	public float additionalWeaponsShootingTime = 5f;

	public float reloadAdditionalWeaponsTime = 10f;

	private float additionalWeaponsCurrentTimeToReload;

	private Coroutine additionalShootingInvokeFirst;

	private Coroutine additionalShootingInvokeSecond;

	public float spawnFallTime = 1f;

	public float touchDownDamageRarius = 10f;

	public LayerMask damageLayers;

	public ColliderEvents damageTrigger;

	private GameObject _titanDamageParticles;

	public float currentSpeed;

	public Vector3 currentDirection = Vector3.zero;

	private Vector3 lastPosition = Vector3.zero;

	private Coroutine GetVolocity;

	public TitanTargetDetector TargetDetector
	{
		get
		{
			if (_titanDetector == null)
			{
				_titanDetector = GetComponent<TitanTargetDetector>();
				if (_titanDetector == null)
				{
					_titanDetector = base.gameObject.AddComponent<TitanTargetDetector>();
				}
			}
			if (_titanDetector.myTitan == null)
			{
				_titanDetector.myTitan = this;
			}
			return _titanDetector;
		}
	}

	private Renderer[] allTopRenderers
	{
		get
		{
			if (!renderersCollected)
			{
				_allTopRenderers = topTitanPart.GetComponentsInChildren<Renderer>();
			}
			return _allTopRenderers;
		}
	}

	public float MainWeaponsCurrentTimeToReload
	{
		get
		{
			return mainWeaponsCurrentTimeToReload;
		}
	}

	public float AdditionalWeaponsCurrentTimeToReload
	{
		get
		{
			return additionalWeaponsCurrentTimeToReload;
		}
	}

	private GameObject titanDamageParticles
	{
		get
		{
			if (_titanDamageParticles == null)
			{
				_titanDamageParticles = Object.Instantiate(Resources.Load("Particles/Titanfall/DamageTitan")) as GameObject;
				_titanDamageParticles.transform.SetParent(topTitanPartPosition);
				_titanDamageParticles.transform.localPosition = Vector3.zero;
				_titanDamageParticles.transform.localScale = Vector3.one;
				_titanDamageParticles.SetActive(false);
			}
			return _titanDamageParticles;
		}
	}

	private void ToggleTopRenderers(bool enable)
	{
		Renderer[] array = allTopRenderers;
		foreach (Renderer renderer in array)
		{
			renderer.enabled = enable;
		}
	}

	public override void Awake()
	{
		ToggleTopRenderers(true);
		base.Awake();
		damageTrigger.onTriggerEnter = DamageByDamageTrigger;
		if (!weaponsInitialized)
		{
			foreach (BaseWeapon additionalWeapon in additionalWeapons)
			{
				additionalWeapon.Init(collisionLayer, null, null, base.vehicleColliders);
			}
			foreach (BaseWeapon mainWeapon in mainWeapons)
			{
				mainWeapon.Init(collisionLayer, null, null, base.vehicleColliders);
			}
			weaponsInitialized = true;
		}
		InitialStandart();
		initialArmsRotation = leftArmTransform.localRotation;
		if (base.isMine && GameController.instance != null && GameController.instance.curTypeGame == TypeGame.TitanfallV2)
		{
			ApplyCarUpgradesRPC(base.gameObject.name.Replace("(Clone)", string.Empty));
		}
		FullHealthAndArmor();
	}

	public override void Start()
	{
		base.Start();
		iconMiniMap.SetType("Titan");
		if (settings.offlineMode || base.isMine)
		{
			iconMiniMap.enabled = false;
		}
	}

	private void InitialStandart()
	{
		initialMaxHealth = maxHealth;
		initialMaxSpeed = titanController.moveSpeed;
		foreach (BaseWeapon mainWeapon in mainWeapons)
		{
			initialMainWeapons.Add(mainWeapon.damage);
		}
		foreach (BaseWeapon additionalWeapon in additionalWeapons)
		{
			initialAdditionalWeapons.Add(additionalWeapon.damage);
		}
	}

	public override void OnEnable()
	{
		base.OnEnable();
	}

	private void SetMainWeaponsLastStart(bool value)
	{
		if (!weaponsInitialized)
		{
			return;
		}
		foreach (BaseWeapon mainWeapon in mainWeapons)
		{
			mainWeapon.lastStart = value;
		}
	}

	private void SetAdditionalWeaponsLastStart(bool value)
	{
		if (!weaponsInitialized)
		{
			return;
		}
		foreach (BaseWeapon additionalWeapon in additionalWeapons)
		{
			additionalWeapon.lastStart = value;
		}
	}

	public override void Enable()
	{
		base.enabled = true;
		mainWeaponsCurrentTimeToReload = mainWeaponsMaxTimeToReload;
		additionalWeaponsCurrentTimeToReload = additionalWeaponsShootingTime;
		mainWeaponsCurrentTimeShooting = 0f;
		additWeaponsCurrentTimeShooting = 0f;
		mainWeaponsShooting = false;
		additionalWeaponsShooting = false;
		repeatAdditionalWeaponsShooting = false;
		if (GetVolocity != null)
		{
			StopCoroutine(GetVolocity);
		}
		if (base.isMine || settings.offlineMode)
		{
			titanController.enabled = true;
		}
		GetVolocity = StartCoroutine(GetVolocityIE());
	}

	public override void Disable()
	{
		mainWeaponsShooting = false;
		additionalWeaponsShooting = false;
		repeatAdditionalWeaponsShooting = false;
		mainWeaponsCurrentTimeToReload = mainWeaponsMaxTimeToReload;
		additionalWeaponsCurrentTimeToReload = additionalWeaponsShootingTime;
		mainWeaponsCurrentTimeShooting = 0f;
		additWeaponsCurrentTimeShooting = 0f;
		canShootAdditionalWeapon = true;
		if (base.isMine)
		{
			CarWindow.instance.titanControlls.StopReloadAnimation();
		}
		if (additionalShootingInvokeFirst != null)
		{
			StopCoroutine(additionalShootingInvokeFirst);
			additionalShootingInvokeFirst = null;
		}
		if (additionalShootingInvokeSecond != null)
		{
			StopCoroutine(additionalShootingInvokeSecond);
			additionalShootingInvokeSecond = null;
		}
		if (GetVolocity != null)
		{
			StopCoroutine(GetVolocity);
			GetVolocity = null;
			currentSpeed = 0f;
		}
		base.Disable();
		SetMainWeaponsLastStart(false);
		SetAdditionalWeaponsLastStart(false);
		iconMiniMap.enabled = false;
	}

	public override Transform GetIn(Humanoid player, int n = -1)
	{
		Transform @in = base.GetIn(player, n);
		if (settings.offlineMode || player.photonView.isMine)
		{
			PointerController.instance.RemovePointer(base.transform);
			GameController.instance.switchInterface(true);
			PlayerBehavior.MyPlayer.canMove = true;
			if (GameController.instance.curTypeGame == TypeGame.TitanfallV2)
			{
				TitanSpawner.instance.ResetTitanSpawnRecharge();
			}
		}
		GetControll(player);
		Enable();
		TargetDetector.StartWatchEnemyTitans();
		if (!settings.offlineMode && !player.photonView.isMine)
		{
			iconMiniMap.enabled = true;
		}
		else
		{
			iconMiniMap.enabled = false;
		}
		if (PlayerBehavior.IsMyCurrentVehicle(this))
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Titan_spawn_stats", base.name);
			FlurryWrapper.LogEvent(FlurryWrapper.EV_TITANFALL_STATS, dictionary);
		}
		return @in;
	}

	public override Transform GetOut(Humanoid player, bool withAnim)
	{
		Transform @out = base.GetOut(player, withAnim);
		Disable();
		iconMiniMap.enabled = false;
		moveSource.volume = 0f;
		SetMainWeaponsLastStart(false);
		SetAdditionalWeaponsLastStart(false);
		TargetDetector.StopWatchEnemyTitans();
		return @out;
	}

	public void PropperlyHideTitan(float delay)
	{
		CoroutineRunner.Invoke(delay, delegate
		{
			if (this != null)
			{
				titanController.enabled = false;
				base.transform.position = Vector3.one * -1000f;
			}
		});
	}

	[PunRPC]
	public override void Kill(int iKillerId)
	{
		if (!isDead && !isImmortal)
		{
			if (PlayerBehavior.IsMyCurrentVehicle(this))
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("Titan_death_stats", base.name);
				FlurryWrapper.LogEvent(FlurryWrapper.EV_TITANFALL_STATS, dictionary);
			}
			topMecanimWrapper.SetTrigger("Reset");
			ToggleTopRenderers(false);
			oneShotSource.PlayOneShot(deathAudioClip);
			if (PlayerBehavior.MyPlayer.photonView.viewID == iKillerId)
			{
				CupsManager.Cups += 5;
			}
			bottomMecanimWrapper.SetTrigger("Death");
			base.Kill(iKillerId);
			DamegeArea(100, touchDownDamageRarius);
			SetMainWeaponsLastStart(false);
			SetAdditionalWeaponsLastStart(false);
			if (GameController.instance.curTypeGame == TypeGame.TitanfallV2)
			{
				PropperlyHideTitan(5f);
			}
		}
	}

	[PunRPC]
	public void SetPrivateCarPosition(Vector3 pos, Quaternion rot)
	{
		base.transform.position = pos;
		base.transform.rotation = rot;
	}

	[PunRPC]
	public override void reset()
	{
		jetpackFuel = 100f;
		titanDamageParticles.SetActive(false);
		brockenSource.Stop();
		base.reset();
		titanController.enabled = true;
		ToggleTopRenderers(true);
		topTitanPart.localRotation = Quaternion.identity;
		bottomTitanPart.localRotation = Quaternion.identity;
		bottomMecanimWrapper.SetTrigger("Reset");
		topMecanimWrapper.SetTrigger("Reset");
	}

	public float MainWeaponsMaxDistance()
	{
		if (mainWeapons == null || mainWeapons.Count == 0)
		{
			return 0f;
		}
		return mainWeapons.Max((BaseWeapon weapon) => weapon.maxDistance);
	}

	public float AdditionalWeaponsMaxDistance()
	{
		if (additionalWeapons == null || additionalWeapons.Count == 0)
		{
			return 0f;
		}
		return additionalWeapons.Max((BaseWeapon weapon) => weapon.maxDistance);
	}

	public float MainWeaponsMinDistance()
	{
		if (mainWeapons == null || mainWeapons.Count == 0)
		{
			return 0f;
		}
		return mainWeapons.Min((BaseWeapon weapon) => weapon.minDistance);
	}

	public float AdditionalWeaponsMinDistance()
	{
		if (additionalWeapons == null || additionalWeapons.Count == 0)
		{
			return 0f;
		}
		return additionalWeapons.Min((BaseWeapon weapon) => weapon.minDistance);
	}

	public float MinDistanceForHomingWeapon()
	{
		if (mainWeaponsIsHoming)
		{
			return MainWeaponsMinDistance();
		}
		if (additionalWeaponsIsHoming)
		{
			return AdditionalWeaponsMinDistance();
		}
		return 0f;
	}

	public void ResetMainShootingTime()
	{
		mainWeaponsCurrentTimeShooting = 0f;
	}

	public void ResetAdditionalShootingTime()
	{
		additWeaponsCurrentTimeShooting = 0f;
	}

	private void ShootMainWeapons()
	{
		if ((!settings.offlineMode && !base.photonView.isMine) || mainWeapons.Count == 0)
		{
			return;
		}
		if (mainWeaponsCurrentTimeToReload <= 0f && mainWeapons[0] != null && mainWeapons[0].weaponTypeSpecific != WeaponTypeSpecific.Trebuchet)
		{
			ReloadMainWeapons();
			return;
		}
		mainWeaponsCurrentTimeToReload -= Time.unscaledDeltaTime;
		bool flag = false;
		if (!(mainWeaponsCurrentTimeShooting <= 0f))
		{
			return;
		}
		currentShootingMainWeapon = (currentShootingMainWeapon + 1) % mainWeapons.Count;
		mainWeaponsCurrentTimeShooting = timeBetweenMainWeaponShots;
		if (timeBetweenMainWeaponShots <= 0f)
		{
			foreach (BaseWeapon mainWeapon in mainWeapons)
			{
				flag |= ShootMainWeapon(mainWeapon);
			}
		}
		else
		{
			BaseWeapon weapon = mainWeapons[currentShootingMainWeapon];
			flag = ShootMainWeapon(weapon);
		}
		if (flag && !alreadyShootingMainWeapons)
		{
			if (cycleShootAnimation)
			{
				alreadyShootingMainWeapons = true;
			}
			if (timeBetweenMainWeaponShots <= 0f)
			{
				foreach (string mainWeaponsAnimation in mainWeaponsAnimations)
				{
					topMecanimWrapper.SetTrigger(mainWeaponsAnimation);
				}
			}
			else
			{
				topMecanimWrapper.SetTrigger(mainWeaponsAnimations[currentShootingMainWeapon % mainWeaponsAnimations.Count]);
			}
		}
		if (mainWeapons[0] != null && mainWeapons[0].weaponTypeSpecific == WeaponTypeSpecific.Trebuchet)
		{
			ReloadMainWeapons();
		}
	}

	private bool ShootMainWeapon(BaseWeapon weapon)
	{
		if (TPSCamera.instance.cameraHitPoint != Vector3.zero)
		{
			if (weapon != null)
			{
				weapon.strength = 1f;
				weapon.chargerAmmo = 10;
				weapon.spreadWeapon = weaponSpread;
				weapon.lastStart = true;
				bool homingMissleCatchEnemy;
				Vector3 autoPointForShoot = TargetDetector.GetAutoPointForShoot(true, out homingMissleCatchEnemy);
				return (homingMissleCatchEnemy | !mainWeaponsIsHoming) && weapon.Shoot(autoPointForShoot) != Vector3.zero;
			}
			return false;
		}
		return false;
	}

	private void ReloadMainWeapons()
	{
		if (reloadingMainWeapons)
		{
			return;
		}
		reloadingMainWeapons = true;
		alreadyShootingMainWeapons = false;
		CarWindow.instance.titanControlls.StartReloadMainAmmo(reloadMainWeaponsTime);
		topMecanimWrapper.SetTrigger(mainWeaponReloadAnimation);
		foreach (BaseWeapon mainWeapon in mainWeapons)
		{
			mainWeapon.isReloading = true;
			mainWeapon.RiseEvent(this, "Reload");
		}
		CoroutineRunner.Invoke(reloadMainWeaponsTime, delegate
		{
			mainWeaponsCurrentTimeToReload = mainWeaponsMaxTimeToReload;
			mainWeaponsCurrentTimeShooting = 0f;
			reloadingMainWeapons = false;
			foreach (BaseWeapon mainWeapon2 in mainWeapons)
			{
				mainWeapon2.isReloading = false;
				mainWeapon2.RiseEvent(this, "EndReload");
			}
		});
	}

	private void ShootAdditionalWeapons()
	{
		if ((!settings.offlineMode && !base.photonView.isMine) || additionalWeapons.Count == 0)
		{
			return;
		}
		additWeaponsCurrentTimeShooting -= Time.unscaledDeltaTime;
		bool flag = false;
		if (!(additWeaponsCurrentTimeShooting <= 0f))
		{
			return;
		}
		currentShootingAdditionalWeapon = (currentShootingAdditionalWeapon + 1) % additionalWeapons.Count;
		additWeaponsCurrentTimeShooting = timeBetweenAdditWeaponShots;
		if (timeBetweenAdditWeaponShots <= 0f)
		{
			foreach (BaseWeapon additionalWeapon in additionalWeapons)
			{
				flag |= ShootAdditWeapon(additionalWeapon);
			}
		}
		else
		{
			BaseWeapon weapon = additionalWeapons[currentShootingAdditionalWeapon];
			flag = ShootAdditWeapon(weapon);
		}
		if (!flag)
		{
			return;
		}
		if (timeBetweenMainWeaponShots <= 0f)
		{
			foreach (string additionalWeaponsAnimation in additionalWeaponsAnimations)
			{
				topMecanimWrapper.SetTrigger(additionalWeaponsAnimation);
			}
			return;
		}
		topMecanimWrapper.SetTrigger(additionalWeaponsAnimations[currentShootingAdditionalWeapon % additionalWeaponsAnimations.Count]);
	}

	private bool ShootAdditWeapon(BaseWeapon weapon)
	{
		if (TPSCamera.instance.cameraHitPoint != Vector3.zero)
		{
			if (weapon != null)
			{
				weapon.strength = 1f;
				weapon.chargerAmmo = 10;
				weapon.spreadWeapon = weaponSpread;
				bool homingMissleCatchEnemy;
				Vector3 autoPointForShoot = TargetDetector.GetAutoPointForShoot(false, out homingMissleCatchEnemy);
				return (homingMissleCatchEnemy | !additionalWeaponsIsHoming) && weapon.Shoot(autoPointForShoot) != Vector3.zero;
			}
			return false;
		}
		return false;
	}

	public void StartShootingAdditionalWeapons()
	{
		if (!canShootAdditionalWeapon)
		{
			return;
		}
		canShootAdditionalWeapon = false;
		additionalWeaponsShooting = true;
		additionalShootingInvokeFirst = CoroutineRunner.Invoke(reloadAdditionalWeaponsTime, delegate
		{
			foreach (BaseWeapon additionalWeapon in additionalWeapons)
			{
				additionalWeapon.RiseEvent(this, "EndReload");
			}
			canShootAdditionalWeapon = true;
			additionalWeaponsCurrentTimeToReload = additionalWeaponsShootingTime;
			additionalShootingInvokeFirst = null;
		});
		additionalShootingInvokeSecond = CoroutineRunner.Invoke(additionalWeaponsShootingTime, delegate
		{
			additionalWeaponsShooting = false;
			foreach (string additionalWeaponsAnimation in additionalWeaponsAnimations)
			{
				topMecanimWrapper.ResetTrigger(additionalWeaponsAnimation);
			}
			topMecanimWrapper.SetTrigger(additionalWeaponReloadAnimation);
			foreach (BaseWeapon additionalWeapon2 in additionalWeapons)
			{
				additionalWeapon2.RiseEvent(this, "Reload");
			}
			additionalShootingInvokeSecond = null;
		});
		StartCoroutine(AdditionalWeaponTimeCoroutine());
	}

	private IEnumerator AdditionalWeaponTimeCoroutine()
	{
		while (additionalWeaponsCurrentTimeToReload > 0f)
		{
			additionalWeaponsCurrentTimeToReload -= Time.unscaledDeltaTime;
			yield return null;
		}
		CarWindow.instance.titanControlls.StartReloadAdditionalAmmo(Mathf.Max(0f, reloadAdditionalWeaponsTime - additionalWeaponsShootingTime));
		yield return null;
	}

	public void DamegeArea(int damage, float radius)
	{
		if (PlayerBehavior.IsMyCurrentVehicle(this))
		{
			Collider[] array = Physics.OverlapSphere(base.transform.position, radius, damageLayers);
			Collider[] array2 = array;
			foreach (Collider other in array2)
			{
				ApplyDamageToColliderOwner(other, damage);
			}
		}
	}

	public void SpawnTitanAnimations()
	{
		oneShotSource.PlayOneShot(fallAudioClip);
		Vector3 finalPosition = Vector3.zero;
		RaycastHit hit = default(RaycastHit);
		if (base.isMine)
		{
			if (CameraModeControl.instance.mainCameraType == settings.MainCameraType.FPSCamera)
			{
				CameraModeControl.instance.EnableTpsCamera(false, true);
			}
			PlayerBehavior.MyPlayer.canMove = false;
		}
		if (Physics.Raycast(base.transform.position - Vector3.up * 0.5f, Vector3.down, out hit, 300f, GameController.instance.collisionGround))
		{
			finalPosition = hit.point;
		}
		bottomMecanimWrapper.SetTrigger("Reset");
		topMecanimWrapper.SetTrigger("Reset");
		HOTween.To(base.transform, spawnFallTime, new TweenParms().Prop("position", finalPosition).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			TitanSpawner.instance.spawnTitanParticles.transform.position = hit.point + Vector3.up * 0.1f;
			TitanSpawner.instance.spawnTitanParticles.transform.up = hit.normal;
			TitanSpawner.instance.spawnTitanParticles.Play();
			bottomMecanimWrapper.SetTrigger("Respawn");
			topMecanimWrapper.SetTrigger("Respawn");
		}));
		float getInRunTime = spawnFallTime + 0.5f;
		PlayerBehavior.MyPlayer.transform.forward = base.transform.forward;
		HOTween.To(PlayerBehavior.MyPlayer.transform, getInRunTime * 0.6f, new TweenParms().Prop("position", finalPosition - base.transform.forward * 2.5f).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			PlayerBehavior.MyPlayer.canMove = true;
			PlayerBehavior.MyPlayer.tController.lastJumpButtonTime = Time.time;
			PlayerBehavior.MyPlayer.tController.jumping = true;
			CoroutineRunner.Invoke(0.2f, delegate
			{
				HOTween.To(PlayerBehavior.MyPlayer.transform, getInRunTime * 0.4f, new TweenParms().Prop("position", finalPosition - base.transform.forward * 1f + base.transform.up * 2.5f).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					PlayerBehavior.MyPlayer.GetInCarWithConfirmation(PlayerBehavior.MyPlayer.viewId, base.viewId);
				}));
			});
		}));
	}

	public void SpawnTitanImmediately()
	{
		Vector3 position = Vector3.zero;
		RaycastHit hitInfo = default(RaycastHit);
		if (base.isMine)
		{
			if (CameraModeControl.instance.mainCameraType == settings.MainCameraType.FPSCamera)
			{
				CameraModeControl.instance.EnableTpsCamera(false, true);
			}
			PlayerBehavior.MyPlayer.canMove = false;
		}
		if (Physics.Raycast(base.transform.position - Vector3.up * 0.5f, Vector3.down, out hitInfo, 300f, GameController.instance.collisionGround))
		{
			position = hitInfo.point;
		}
		bottomMecanimWrapper.SetIsGrounded(true);
		base.transform.position = position;
	}

	public override int GetBodyHeight()
	{
		return (base.BodyHeight != 0) ? base.BodyHeight : 8;
	}

	private void SetDamageTrigger()
	{
		damageTrigger.transform.rotation = topTitanPart.rotation;
		damageTrigger.transform.position = base.transform.position + currentDirection;
	}

	private void DamageByDamageTrigger(Collider other)
	{
		if (PlayerBehavior.IsMyCurrentVehicle(this) && !(currentSpeed < 1f) && !isFree)
		{
			ApplyDamageToColliderOwner(other, 101);
		}
	}

	private void ApplyDamageToColliderOwner(Collider other, int damage)
	{
		if (!settings.offlineMode && GameController.instance.curTypeGame == TypeGame.WalkingRobots)
		{
			return;
		}
		Mortal componentInParent = other.GetComponentInParent<Mortal>();
		if (componentInParent == null || componentInParent == this || componentInParent == PlayerBehavior.MyPlayer)
		{
			return;
		}
		PlayerBehavior playerBehavior = GameController.instance.FindPlayer(componentInParent.photonView.ownerId);
		if ((componentInParent is PlayerBehavior || componentInParent is EntityBehavior) && playerBehavior.localTeam != 0 && TDMController.myTeam == playerBehavior.localTeam)
		{
			return;
		}
		if (componentInParent is EnemyBehavior)
		{
			(componentInParent as EnemyBehavior).highDamageCar(5);
		}
		if (componentInParent is PlayerBehavior && componentInParent as PlayerBehavior != PlayerBehavior.MyPlayer && !(componentInParent as PlayerBehavior).inCar)
		{
			(componentInParent as PlayerBehavior).photonView.RPC("DamageCar", PhotonTargets.All, damage, PlayerBehavior.MyPlayer.viewId, true);
		}
		if (componentInParent is EntityBehavior)
		{
			componentInParent.photonView.RPC("GetDamage", PhotonTargets.All, damage, 3, PlayerBehavior.MyPlayer.viewId);
			if (impactClips.Length > 0)
			{
				oneShotSource.PlayOneShot(impactClips[Random.Range(0, impactClips.Length)]);
			}
		}
	}

	[PunRPC]
	public override void StartDamageParticle()
	{
	}

	public override void GetDamage(int damage, int damageType, int iKillerId, Vector3 killerPosition, Vector3? hitPoint)
	{
		if (isImmortal)
		{
			return;
		}
		base.GetDamage(damage, damageType, iKillerId, killerPosition, hitPoint);
		if (!base.isMine)
		{
			return;
		}
		if ((float)health <= (float)maxHealth * 0.25f)
		{
			titanDamageParticles.SetActive(true);
			if (!brockenSource.isPlaying)
			{
				brockenSource.Play();
			}
		}
		else
		{
			titanDamageParticles.SetActive(false);
			if (brockenSource.isPlaying)
			{
				brockenSource.Stop();
			}
		}
	}

	private void Update()
	{
		if (settings.offlineMode || base.isMine)
		{
			float num = 0f;
			if (titanController.isGrounded)
			{
				if (!moveSource.isPlaying)
				{
					moveSource.Play();
				}
				num = ((!base.isMine) ? (currentSpeed * 0.5f) : currentSpeed);
			}
			else
			{
				num = 0f;
			}
			moveSource.volume = Mathf.Lerp(moveSource.volume, num, Time.deltaTime * 3f);
			moveSource.pitch = Mathf.Clamp(Mathf.Lerp(moveSource.pitch, currentSpeed, Time.deltaTime * 2f), 0.8f, 1f);
		}
		if (settings.offlineMode || base.isMine || Device.indexWeakDevice == 0)
		{
			if (titanController.usingJetpack && (jetpackFuel > 1f || (!settings.offlineMode && !ownerPhotonView.isMine)))
			{
				ParticleSystem[] array = jetpackParticles;
				foreach (ParticleSystem particleSystem in array)
				{
					if (!particleSystem.isPlaying)
					{
						particleSystem.Play();
					}
					if (!jatpackSource.isPlaying)
					{
						jatpackSource.Play();
					}
					jatpackSource.volume = Mathf.Lerp(jatpackSource.volume, 1f, Time.deltaTime);
				}
			}
			else
			{
				ParticleSystem[] array2 = jetpackParticles;
				foreach (ParticleSystem particleSystem2 in array2)
				{
					if (particleSystem2.isPlaying)
					{
						particleSystem2.Stop();
					}
					jatpackSource.volume = Mathf.Lerp(jatpackSource.volume, 0f, Time.deltaTime * 2f);
					if (jatpackSource.isPlaying && jatpackSource.volume <= 0.01f)
					{
						jatpackSource.Stop();
					}
				}
			}
		}
		if (mainWeaponsCurrentTimeShooting > 0f)
		{
			mainWeaponsCurrentTimeShooting -= Time.unscaledDeltaTime;
			mainWeaponsCurrentTimeShooting = Mathf.Max(0f, mainWeaponsCurrentTimeShooting);
		}
		if (mainWeaponsShooting && !reloadingMainWeapons)
		{
			ShootMainWeapons();
		}
		if (!mainWeaponsShooting && alreadyShootingMainWeapons)
		{
			alreadyShootingMainWeapons = false;
		}
		if (!settings.offlineMode && !base.photonView.isMine)
		{
			SetMainWeaponsLastStart(mainWeaponsShooting);
			return;
		}
		if (additionalWeaponsShooting)
		{
			ShootAdditionalWeapons();
		}
		if (repeatAdditionalWeaponsShooting)
		{
			StartShootingAdditionalWeapons();
		}
		if (!settings.offlineMode && !base.photonView.isMine)
		{
			SetAdditionalWeaponsLastStart(additionalWeaponsShooting);
		}
		else if (PlayerBehavior.IsMyCurrentVehicle(this))
		{
			SetDamageTrigger();
			if (jetpackFuel < 100f && titanController.isGrounded)
			{
				jetpackFuel += fillJetpackFuelWithTime * Time.deltaTime;
			}
		}
	}

	private IEnumerator GetVolocityIE()
	{
		while (true)
		{
			yield return new WaitForSeconds(0.1f);
			currentDirection = (base.transform.position - lastPosition).normalized;
			float velocityMagnitude = (base.transform.position - lastPosition).magnitude;
			if (velocityMagnitude < 150f)
			{
				currentSpeed = velocityMagnitude * 10f / titanController.moveSpeed;
			}
			lastPosition = base.transform.position;
		}
	}

	public override void ApplyCarUpgradesRPC(string titanName)
	{
		float carUpgradeCoef = MarketController.Instance.GetCarUpgradeCoef(titanName, UpgradeType.Body);
		float carUpgradeCoef2 = MarketController.Instance.GetCarUpgradeCoef(titanName, UpgradeType.Engine);
		float carUpgradeCoef3 = MarketController.Instance.GetCarUpgradeCoef(titanName, UpgradeType.Intake);
		float carUpgradeCoef4 = MarketController.Instance.GetCarUpgradeCoef(titanName, UpgradeType.Wheels);
		ProductVehicleSkin equipedVehicleSkin = MarketController.Instance.GetEquipedVehicleSkin(titanName);
		string text = ((equipedVehicleSkin != null && !(equipedVehicleSkin.skinTextureName == string.Empty)) ? equipedVehicleSkin.skinTextureName : string.Empty);
		if (PhotonNetwork.connected)
		{
			base.photonView.RPC("ApplyCarUpgrades", PhotonTargets.All, carUpgradeCoef2, carUpgradeCoef3, carUpgradeCoef, carUpgradeCoef4, text);
			PhotonNetwork.player.UpdateProperty(PlayerPropsKeys.carSkin, text);
		}
		else
		{
			ApplyCarUpgrades(carUpgradeCoef2, carUpgradeCoef3, carUpgradeCoef, carUpgradeCoef4, text);
		}
	}

	[PunRPC]
	public void ApplyCarUpgrades(float maxSpeedCoef, float weaponMain, float maxHealthCoef, float additionalWeapon, string newSkin)
	{
		maxHealth = (int)((float)initialMaxHealth * maxHealthCoef);
		titanController.moveSpeed = initialMaxSpeed * maxSpeedCoef;
		for (int i = 0; i < mainWeapons.Count; i++)
		{
			mainWeapons[i].damage = initialMainWeapons[i] * weaponMain;
		}
		for (int j = 0; j < additionalWeapons.Count; j++)
		{
			additionalWeapons[j].damage = initialAdditionalWeapons[j] * additionalWeapon;
		}
		if (base.isMine && PrivateCarManager.instance != null)
		{
			PrivateCarManager.instance.slots[0].LoadAndAddTexture(this);
		}
	}
}
